package com.hualan.sfzy.controller;

import com.hualan.sfzy.model.dto.FmDto;
import com.hualan.sfzy.model.entity.Fm;
import com.hualan.sfzy.model.vo.FmQuery;
import com.hualan.sfzy.service.FmService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.hualan.sfzy.constant.CommonConstant.*;

/**
 * FM管理控制器
 */
@RestController
@RequestMapping("/fm")
public class FmController {

    @Autowired
    private FmService fmService;

    /**
     * 添加FM
     */
    @PostMapping("/add")
    public Result add(@RequestBody Fm fm) {
        int result = fmService.add(fm);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 编辑FM
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody Fm fm) {
        int result = fmService.edit(fm);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 删除FM
     */
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Integer id) {
        int result = fmService.remove(id);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 批量删除FM
     */
    @DeleteMapping("/removeBatch")
    public Result removeBatch(@RequestBody Integer[] ids) {
        int result = fmService.removeBatch(ids);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 查询所有FM
     */
    @GetMapping("/find")
    public Result find() {
        List<FmDto> fms = fmService.findAll();
        return Result.success(fms);
    }

    /**
     * 分页查询FM
     */
    @GetMapping("/findPage")
    public Result findPage(@RequestParam(value = PAGE_NUM, defaultValue = PAGE_NUM_DEFAULT) Integer pageNum,
                          @RequestParam(value = PAGE_SIZE, defaultValue = PAGE_SIZE_DEFAULT) Integer pageSize,
                          Fm fm) {
        Map<String, Object> result = fmService.findPage(pageNum, pageSize, fm);
        return Result.success(result);
    }

    /**
     * 根据ID查询FM
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {
        Fm fm = fmService.findById(id);
        return Result.success(fm);
    }

    /**
     * 分页条件查询FM
     */
    @GetMapping("/findFmPage")
    public Result findFmPage(@RequestParam(value = PAGE_NUM, defaultValue = PAGE_NUM_DEFAULT) Integer pageNum,
                            @RequestParam(value = PAGE_SIZE, defaultValue = PAGE_SIZE_DEFAULT) Integer pageSize,
                            FmQuery query) {
        Map<String, Object> result = fmService.findFmPage(pageNum, pageSize, query);
        return Result.success(result);
    }
} 